package com.tony.ai.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding

abstract class BaseListAdapter<T, VB : ViewBinding>(
    private val inflateBinding: (LayoutInflater, ViewGroup, Boolean) -> VB
) : RecyclerView.Adapter<BaseListAdapter.BaseViewHolder<VB>>() {

    private var items: List<T> = emptyList()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<VB> {
        val inflater = LayoutInflater.from(parent.context)
        val binding = inflateBinding(inflater, parent, false)
        return BaseViewHolder(binding)
    }

    override fun onBindViewHolder(holder: BaseViewHolder<VB>, position: Int) {
        val item = items[position]
        bind(holder.binding, item, position)
    }

    override fun getItemCount(): Int = items.size

    abstract fun bind(binding: VB, item: T, position: Int)

    fun setData(newItems: List<T>) {
        items = newItems
        notifyDataSetChanged()
    }

    class BaseViewHolder<VB : ViewBinding>(val binding: VB) : RecyclerView.ViewHolder(binding.root)
}